package com.rover022.tool.view
{
	import com.bit101.components.ScrollPane;
	
	import flash.desktop.ClipboardFormats;
	import flash.desktop.NativeDragManager;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.NativeDragEvent;
	import flash.filesystem.File;
	
	public class ImageGallery extends Sprite
	{
		private var _imageItems:Vector.<ImageItem>;
		private var _width:Number = 900;
		private var _height:Number = 800;
		private var _colCount:int = 0;
		private var _rowCount:int = 0;
		private var _colSpan:Number = 0;
		private var _sortByName:Boolean = false;
		private var _scrollPosY:Number = 0;
		private var _scrollPanel:ScrollPane;
		public var itemContainer:Sprite
		
		
		public function ImageGallery()
		{
			super();
			_imageItems = new Vector.<ImageItem>();
			this.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, onDragEnter);
			this.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onDragDrop);
			this.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
			this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			this.graphics.beginFill(0x0,0.1);
			this.graphics.drawEllipse(0,0,_width,_height);
			this.graphics.endFill();
			_scrollPanel = new ScrollPane(this,0,0);
			_scrollPanel.setSize(_width,_height);
			itemContainer = new Sprite();
			_scrollPanel.addChild(itemContainer);
		}
		
 
		
		
		
		/**
		 * 设置道具大小
		 */
		public function setImageSize(w:uint,h:uint):void{
			
		}
		
		/**
		 * removeALlItem
		 */
		public function removeAll():void{
			
		}
		/**
		 * 显示关系图
		 */
		public function showLink():void{
			
		}
		/**
		 * 关闭关系图
		 */
		public function closeLink():void{
			
		}
		
		
		
		public function get imageItems():Vector.<ImageItem>
		{
			return _imageItems;
		}
		
		public override function set width(value:Number):void
		{
			setSize(value, _height);
		}
		
		public override function set height(value:Number):void
		{
			setSize(_width, value);
		}
		
		public function setSize(width:Number, height:Number):void
		{
			_width = width;
			_height = height;
			
			graphics.clear();
			graphics.beginFill(0xffa0a0a0, 1.0);
			graphics.drawRect(0, 0, _width, _height);
			graphics.endFill();
			
			var maskBmpDat:BitmapData = new BitmapData(width, height, true, 0xffffffff);
			super.mask = new Bitmap(maskBmpDat);
			
			adjustLayout();
		}
		
		public function get contentWidth():Number
		{
			return ImageItem.boundWidth * _colCount + _colSpan * (_colCount + 1);
		}
		
		public function get contentHeight():Number
		{
			return ImageItem.boundHeight * _rowCount;
		}
		
		public function get loadComplete():Boolean
		{
			if (_imageItems.length == 0)
				return false;
			
			for each (var imgItm:ImageItem in _imageItems)
			{
				if (!imgItm.loaded)
				{
					return false;
				}
			}
			return true;
		}
		
		private function onDragEnter(evt:NativeDragEvent):void
		{
			NativeDragManager.acceptDragDrop(this);
		}
		
		private function onDragDrop(evt:NativeDragEvent):void
		{
			var fileList:Object = evt.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT);
			var fileNum:int = fileList.length;
			for (var index:int = 0; index < fileNum; ++index)
			{
				var file:File = File(fileList[index]);
				if (file.isDirectory)
				{
					var subFileList:Array = file.getDirectoryListing();
					for each (var subFile:File in subFileList)
					{
						if (!subFile.isDirectory)
							importFile(subFile);
					}
				}
				else
				{
					importFile(file);
				}
			}
			
			adjustLayout();
		}
		
		private function importFile(file:File):Boolean
		{
			var fileType:String = file.nativePath.substring(file.nativePath.lastIndexOf(".") + 1).toLowerCase();
			if ("png" == fileType || "jpg" == fileType || "swf" == fileType || "swv" == fileType || "bmp" == fileType || "jpeg" == fileType)
			{
				var imgItm:ImageItem = new ImageItem(this);
				imgItm.load(file.url);
				this.addChild(imgItm);
				_imageItems.push(imgItm);
				return true;
			}
			
			return false;
		}
		
		internal function onItemLoaded(item:ImageItem):void
		{
			sortItems();
		}
		
		public function sortItems():void
		{
			if (_sortByName)
				_imageItems.sort(compareName);
			else
				_imageItems.sort(compareSize);
			
			adjustLayout();
		}
		
		private static function compareSize(left:ImageItem, right:ImageItem):Number
		{
			var heightDiff:Number = right.contentHeight - left.contentHeight;
			return (heightDiff != 0 ? heightDiff : (right.contentWidth - left.contentWidth));
		}
		
		private static function compareName(left:ImageItem, right:ImageItem):Number
		{
			return (left.name < right.name ? -1 : 1);
		}
		
		public function clear():void
		{
			super.removeChildren();
			for each (var imgItm:ImageItem in _imageItems)
			{
				imgItm.dispose();
			}
			_imageItems.splice(0, _imageItems.length);
			
			_colCount = 0;
			_rowCount = 0;
			_colSpan = 0;
			_scrollPosY = 0;
		}
		
		private function onMouseWheel(evt:MouseEvent):void
		{
			var newPosY:Number = adjustScrollPos(10 * evt.delta);
			var deltaScrollPosY:Number = newPosY - _scrollPosY;
			_scrollPosY = newPosY;
			for (var index:int = 0; index < _imageItems.length; ++index)
			{
				var imgItm:ImageItem = _imageItems[index];
				imgItm.y += deltaScrollPosY;
			}
		}
		
		private function adjustScrollPos(delta:Number):Number
		{
			var newPosY:Number = _scrollPosY + delta;
			if (newPosY < ImageItem.boundHeight - contentHeight)
				newPosY = ImageItem.boundHeight - contentHeight;
			else if (newPosY > _height - ImageItem.boundHeight)
				newPosY = _height - ImageItem.boundHeight;
			
			return newPosY;
		}
		
		private function adjustLayout():void
		{
			_colCount = Math.floor(_width / ImageItem.boundWidth);
			_rowCount = Math.ceil(Number(_imageItems.length) / Number(_colCount));
			_colSpan = (_width - _colCount * ImageItem.boundWidth) / (_colCount + 1);
			_scrollPosY = adjustScrollPos(0);
			for (var index:int = 0; index < _imageItems.length; ++index)
			{
				var imgItm:ImageItem = _imageItems[index];
				imgItm.x = _colSpan + (ImageItem.boundWidth + _colSpan) * (index % _colCount);
				imgItm.y = _scrollPosY + ImageItem.boundHeight * Math.floor(index / _colCount);
			}
		}
		
		private function onMouseUp(evt:MouseEvent):void
		{
			if (evt.ctrlKey && evt.altKey)
			{
				_sortByName = !_sortByName;
				sortItems();
			}
		}
	}
}